package com.think.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.think.teaching.mapper.StudentInfoMapper;
import com.think.teaching.model.StudentInfo;
import com.think.teaching.service.StudentInfoService;

import java.util.Map;

import org.springframework.stereotype.Service;

/**
 * <p>
 * 学生详细信息 服务实现类
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@Service
public class StudentInfoServiceImpl extends BaseServiceImpl<StudentInfoMapper, StudentInfo>
		implements StudentInfoService {

	@Override
	public StudentInfo AddStudentInfo(StudentInfo studentInfo) {
		if (this.save(studentInfo)) {
			return studentInfo;
		}
		return null;
	}

	@Override
	public StudentInfo UpdateStudentInfo(StudentInfo studentInfo) {

		if (this.updateById(studentInfo)) {
			return studentInfo;
		}
		return null;
	}

	@Override
	public StudentInfo GetStudentInfo(Integer id) {
		QueryWrapper<StudentInfo> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id", id).eq("state", 1).ne("delflag", 1);
		return getOne(queryWrapper);
	}

	public StudentInfo GetStudentInfoByUserId(Integer id) {
		QueryWrapper<StudentInfo> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("user_id", id).eq("state", 1).ne("delflag", 1);
		return getOne(queryWrapper);
	}

	public IPage<Map<String, Object>> ListStudentInfo(Integer page, Integer count, Integer state, Integer classId,
			Integer major, Integer college, Integer school) {

		IPage<Map<String, Object>> ipage = new Page<>(page, count);
		if (classId > 0) {
			return this.baseMapper.listStudentInfo(ipage, state, classId,-1,-1,-1);
		} else if (major > 0) {
			return this.baseMapper.listStudentInfo(ipage, state, -1,major,-1,-1);

		} else if (college > 0) {
			return this.baseMapper.listStudentInfo(ipage, state, -1,-1,college,-1);

		} else if (school > 0) {
			return this.baseMapper.listStudentInfo(ipage, state, -1,-1,-1,school);
		}
		return this.baseMapper.listStudentInfo(ipage, state, -1,-1,-1,-1);

	}

	@Override
	public StudentInfo SetStudentState(Integer student, Integer state) {
		UpdateWrapper<StudentInfo> queryWrapper = new UpdateWrapper<>();
		queryWrapper.eq("id", student).ne("delflag", 1).set("state", state);
		this.update(queryWrapper);
		return getById(student);
	}

}
